﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Text.RegularExpressions;


/// <summary>
///CommonMethod 的摘要说明
/// </summary>
public class CommonMethod
{
    private CommonMethod()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
        
    }
    

    /// <summary>
    /// 单例模式
    /// </summary>
    private static CommonMethod _commonMethod;
    public static CommonMethod Instance
    {
        get
        {
            if (_commonMethod == null)
                _commonMethod = new CommonMethod();
            return _commonMethod;
        }
    }

    /// <summary>
    /// 根据userAgent获取手机操作系统及版本
    /// </summary>
    /// <param name="userAgent"></param>
    /// <param name="osType"></param>
    /// <param name="osVersion"></param>
    public void getClientType(string userAgent, out string osType, out string osVersion)
    {
        osType = string.Empty;
        osVersion = string.Empty;
        //电脑客户端
        if (userAgent != null && userAgent.IndexOf("mozilla") != -1)
            osType = "PC";
        //手机客户端
        else
        {
            int subStart = 0;
            int subEnd = 0;
            //如果是android客户端
            if (userAgent.IndexOf("android") != -1)
            {
                subStart = userAgent.IndexOf('(');
                subEnd = userAgent.IndexOf(';');
                string mobileInfo = userAgent.Substring(subStart + 1, subEnd - subStart - 1);
                osType = "android";
                osVersion = mobileInfo.Replace(osType, "").Trim();
            }
        }
    }

    /// <summary>
    /// 序列化XML
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="strXML"></param>
    /// <returns></returns>
    public T SerializableXML<T>(string strXML) where T : new()
    {
        T obj = new T();// default(T);
        //序列化XML
        XmlSerializer serializer = new XmlSerializer(obj.GetType());
        StringReader Sreader = new StringReader(strXML);
        XmlReader XReader = new XmlTextReader(Sreader);
        obj = (T)serializer.Deserialize(XReader);
        return obj;
    }

    /// <summary>
    /// 加密算法
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public string Encode(string data)
    {

        byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes("19850420");

        byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes("86360934");



        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();

        int i = cryptoProvider.KeySize;

        MemoryStream ms = new MemoryStream();

        CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

        StreamWriter sw = new StreamWriter(cst);

        sw.Write(data);

        sw.Flush();

        cst.FlushFinalBlock();

        sw.Flush();

        return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);

    }

    /// <summary>
    /// 解密算法
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public string Decode(string data)
    {

        byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes("19850420");

        byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes("86360934");

        byte[] byEnc;

        try
        {

            byEnc = Convert.FromBase64String(data);

        }

        catch
        {

            return null;

        }
        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();

        MemoryStream ms = new MemoryStream(byEnc);

        CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);

        StreamReader sr = new StreamReader(cst);

        return sr.ReadToEnd();

    }

    /// <summary>
    /// 去除HTML标签/样式/脚本
    /// </summary>
    /// <param name="Htmlstring"></param>
    /// <returns></returns>
    public string RemoveSpecifyHtml2(string ctx)
    {

        string[] holdTags = { "a", "img", "br", "strong", "b", "span" };//要保留的 标签

        // <(?!((/?\s?li)|(/?\s?ul)|(/?\s?a)|(/?\s?img)|(/?\s?br)|(/?\s?span)|(/?\s?b)))[^>]+>

        string regStr = string.Format(@"<(?!((/?\s?{0})))[^>]+>", string.Join(@")|(/?\s?", holdTags));

        Regex reg = new Regex(regStr, RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase);

        return reg.Replace(ctx, "");

    }

    /// <summary>
    /// 去除HTML标签/样式/脚本
    /// </summary>
    /// <param name="Htmlstring"></param>
    /// <returns></returns>
    public string RemoveHtmlTag(string Htmlstring)
    {
 



        //删除脚本
        Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);

        //删除样式
        Htmlstring = Regex.Replace(Htmlstring, @" <style(\s[^>]*?)?>[\s\S]*? </style>", "", RegexOptions.IgnoreCase);


        //删除HTML
        Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);

        Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "<br>", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"<img[^>]*>;", "", RegexOptions.IgnoreCase);
        Htmlstring.Replace("<", "");
        Htmlstring.Replace(">", "");
        Htmlstring.Replace("\r\n", "");


        return Htmlstring;
    }
    public bool checkTextBox(string txtContent)
    {
        bool b = false;
        if (txtContent.Contains('\'')||txtContent.Contains('<'))
        {
            b = true;
        }
        return b;
    }
}
